package com.example.micro.aop.login.service;

import com.example.micro.aop.login.entity.User;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    private final List<User> userList = new ArrayList<>();

    //内置一部分已经注册的用户信息
    public void getUserlist() {
        User user1 = new User("1", "王五", "22");
        User user2 = new User("2", "limin", "22");
        User user3 = new User("3", "limi", "22");
        User user4 = new User("4", "lim", "22");
        User user5 = new User("5", "li", "22");
        this.userList.add(user1);
        this.userList.add(user2);
        this.userList.add(user3);
        this.userList.add(user4);
        this.userList.add(user5);
    }

    public UserServiceImpl() {
        getUserlist();
    }

    @Override
    public User getUser(String id) {

        User user = null;
        for (User usertest : userList) {
            if (usertest.getId().equals(id)) {
                user = usertest;
            }
        }
        return user;
    }

    @Override
    public User getUserByName(String username, String password) {

        for (User user : userList) {
            if (user.getName().equals(username) && user.getPassword().equals(password)) {
                return user;
            }
        }
        return null;
    }
}

